<template>
  <div class="music-list">
    <div class="back" @click="goBack">
      <i class="iconfont icon-back"></i>
    </div>
    <h1 class="title" v-html="title"></h1>
    <div class="bg-image" :style="bgStyle" ref="bgImage">
      <div class="play-wrapper" ref="playWrapper">
        <div class="play" v-show="songs.length>0" ref="playBtn" @click="playRandom">
          <i class="iconfont icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <div class="filter" ref="filter"></div>
    </div>
    <div class="bg-layer" ref="bglayer"></div>
    <scroll
      :data="songs"
      class="list"
      :probe-type="probeType"
      :listenScroll="listenScroll"
      @scroll="ScrollList"
      ref="list"
    >
      <div class="song-list-wrapper">
        <songlist :rank="rank" :songs="songs" @toplay="playMusic" @noplay="NotMusic"></songlist>
      </div>
      <div class="loading-container" v-show="!songs.length">
        <loading></loading>
      </div>
    </scroll>
     <Confirm ref="Confirm" :text="content" confirmBtnText="支付"></Confirm>
  </div>
</template>
<script>
import songlist from "@/base/song-list/song-list";
import scroll from "@/base/scroll/scroll";
import { prefixStyle } from "@/common/js/dom";
import loading from "@/base/loading/loading";
import { mapActions, mapMutations } from "vuex";
import { getMusicVkey } from "../../api/Singer";
import { createSong } from "@/common/js/SongObject";
import { ERR_OK } from "@/api/config";
import { playlistMixin } from "@/common/js/mixin";
import Confirm from "../../base/confirm/confirm"
// 歌手背景图的偏移量
const RESET_VISE = 40;
//
// const transform = prefixStyle('transform')
// const backdrop = prefixStyle('backdrop-filter')

export default {
  mixins: [playlistMixin],
  data() {
    return {
      scrollY: 0, //向上滚动了的高度
      tipTag: false, //弹窗
      content: "" //弹窗的文字
    };
  },
  props: {
    bgImage: {
      type: String,
      default: ""
    },
    songs: {
      type: Array,
      default: []
    },
    title: {
      type: String,
      default: ""
    },
    rank: {
      type: Boolean,
      default: false
    }
  },
  components: {
    scroll,
    songlist,
    loading,
    Confirm
  },
  computed: {
    //根据父组件传过来的bgImage计算出背景属性
    bgStyle() {
      return `background-image:url(${this.bgImage})`;
    }
  },
  created() {
    this.probeType = 3;
    this.listenScroll = true;
  },
  mounted() {
    // 将歌手图片的位置的高度存在this,这个对象中
    this.imageHeight = this.$refs.bgImage.clientHeight;
    this.minTranslateY = -this.imageHeight + 40;
    this.$refs.list.$el.style.top = `${this.imageHeight}px`;
  },
  methods: {
    //mixin
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? "60px" : "";
      this.$refs.list.$el.style.bottom = bottom;
      this.$refs.list.refresh();
    },
    //点击随机播放按钮，播放音乐
    playRandom() {
      this.randomPlay({
        list: this.songs
      });
    },
    //滚动事件
    ScrollList(pos) {
      this.scrollY = pos.y;
    },
    //点击，返回上一层页面
    goBack() {
      this.$router.back();
    },
    // songlist的点击事件
    playMusic(obj) {
      this.selectPlay({
        list: this.songs,
        index: obj.index
      });
      // console.log(obj)
      // let selectSong = this.songs[obj.index];
      //  this.changePlaying(true)
      //  this._initMusicVkey(selectSong.mid)
    },
    //处理没有歌曲播放权限
    NotMusic(e) {
      console.log(e);
      this.$refs.Confirm.show()
      this.content = e;
    },
    //关闭弹窗
    closeTips(e) {
      this.tipTag = false;
    },
    //获取歌曲的播放源
    _initMusicVkey(mid) {
      let playUrl = "";
      getMusicVkey(mid).then(res => {
        console.log(res);
        let Data = res.req_0;
        if (Data.code === ERR_OK) {
          playUrl = Data.data.midurlinfo[0].purl;
        }
      });
    },
    ...mapMutations({
      changePlaying: "SET_PLAYING_SATATE"
    }),
    ...mapActions(["selectPlay", "randomPlay"])
  },
  watch: {
    scrollY(newY) {
      let translateY = Math.max(this.minTranslateY, newY);
      let zIndex = 0;
      let scale = 1;
      this.$refs.bglayer.style[
        "transform"
      ] = `translate3d(0,${translateY}px,0)`;
      // 适配多种设备，浏览器
      this.$refs.bglayer.style[
        `webkitTransform`
      ] = `translate3d(0,${translateY}px,0)`;
      //上滚，图片缩小
      if (newY < this.minTranslateY) {
        zIndex = 10;
        this.$refs.bgImage.style.paddingTop = 0;
        this.$refs.bgImage.style.height = `${RESET_VISE}px`;
        this.$refs.playWrapper.style.display = "none";
      } else {
        this.$refs.bgImage.style.paddingTop = `70%`;
        this.$refs.bgImage.style.height = 0;
        this.$refs.playWrapper.style.display = "";
      }
      //下拉图片放大
      const percent = Math.abs(newY / this.imageHeight);
      //制作高斯模糊效果
      let blur = 0;
      if (newY > 0) {
        scale = 1 + percent;
        zIndex = 10;
      } else {
        blur = Math.min(20, percent * 20);
      }
      //图片放大效果
      this.$refs.bgImage.style[`transform`] = `scale(${scale})`;
      // 适配多种设备，浏览器
      this.$refs.bgImage.style[`webkitTransform`] = `scale(${scale})`;
      this.$refs.bgImage.style.zIndex = zIndex;
      //模糊效果
      // this.$refs.filter.style[
      //   'backdrop-filter'
      // ] = `blur(${blur})`;
      // this.$refs.filter.style[
      //   `webkitBackdrop-filter`
      // ] = `blur(${blur})`;
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '../../common/stylus/variable.styl';

.music-list {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: $color-background;

  .back {
    position: absolute;
    top: 0;
    left: 6px;
    z-index: 50;

    .icon-back {
      display: block;
      padding: 10px;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }

  .title {
    position: absolute;
    top: 0;
    left: 10%;
    z-index: 40;
    width: 80%;
    no-wrap();
    text-align: center;
    line-height: 40px;
    font-size: $font-size-large;
    color: $color-text;
  }

  .bg-image {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 70%;
    transform-origin: top;
    background-size: cover;

    .play-wrapper {
      position: absolute;
      bottom: 20px;
      z-index: 50;
      width: 100%;

      .play {
        box-sizing: border-box;
        width: 135px;
        padding: 7px 0;
        margin: 0 auto;
        text-align: center;
        border: 1px solid $color-theme;
        color: $color-theme;
        border-radius: 100px;
        font-size: 0;

        .icon-play {
          display: inline-block;
          vertical-align: middle;
          margin-right: 6px;
          font-size: $font-size-medium-x;
        }

        .text {
          display: inline-block;
          vertical-align: middle;
          font-size: $font-size-small;
        }
      }
    }

    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }

  .bg-layer {
    position: relative;
    height: 100%;
    background: $color-background;
  }

  .list {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 100%;
    background: $color-background;

    .song-list-wrapper {
      padding: 20px 30px;
    }

    .loading-container {
      position: absolute;
      // width: 100%;
      top: 50%;
      left: 50%;
      transform: translate(-50%);
    }
  }
}
</style>
